package cn.wolfcode.web.controller;

import cn.wolfcode.common.web.Result;
import cn.wolfcode.domain.SeckillProduct;
import cn.wolfcode.domain.SeckillProductVo;
import cn.wolfcode.redis.SeckillRedisKey;
import cn.wolfcode.service.ISeckillProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/seckillProduct")
@Slf4j
public class SeckillProductController {
    @Autowired
    private ISeckillProductService seckillProductService;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @RequestMapping("/selectByType")
    public Result<List<SeckillProductVo>> result(@RequestParam("type") int type){
        Result<List<SeckillProductVo>> result = Result.success(seckillProductService.selectByTypes(type));
        return result;
    }
    @RequestMapping("queryByTime")
    public Result<List<SeckillProductVo>> queryByTime(int time){
        return Result.success(seckillProductService.queryByTime(time));
    }

    @RequestMapping("/find")
    public Result<SeckillProductVo> find(int time,Long seckillId){
        SeckillProductVo vo = seckillProductService.queryById(time, seckillId);
        return Result.success(vo);
    }
}
//        String objStr = (String) redisTemplate.opsForHash().get(SeckillRedisKey.SECKILL_REAL_COUNT_HASH.getRealKey(String.valueOf(time)), String.valueOf(seckillId));
//        int currentCount = Integer.parseInt(objStr);
//        vo.setCurrentCount(currentCount>=0?currentCount:0);
